﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
namespace NhaSachDAO
{
   public class HoaDonDAO
    {
        public static DataTable LoadHoaDon()
        {
            SqlConnection cnn = SQLconnection.Hamketnoi();
            SqlCommand cnd = new SqlCommand("select * from HoaDon", cnn);
            cnd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter(cnd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
        public static DataTable LoadHd(string mahd)
        {
            SqlConnection cnn = SQLconnection.Hamketnoi();
            SqlCommand cnd = new SqlCommand("select * from HoaDon Where (MaHD="+mahd+")", cnn);
            cnd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter(cnd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
        public static void ThemHoaDon(string mahd,string nhanvienlap)
        {
            SqlConnection cnn = SQLconnection.Hamketnoi();
            SqlCommand cmd = new SqlCommand("Insert into HoaDon(MAHD,TongTien,NhanVienLap,manv,makh) Values ('"+mahd+"',0,N'" + nhanvienlap + "',1,11)", cnn);
            cmd.CommandType = CommandType.Text;
            cnn.Open();
            cmd.ExecuteNonQuery();
            cnn.Close();
        }
        public static void CapNhatHD(string mahd)
        {
            SqlConnection cnn = SQLconnection.Hamketnoi();
            SqlCommand cmd = new SqlCommand("Update HoaDon set TongTien = (select sum(thanhtien) from chitiethoadon where mahd="+mahd+")where MaHD like '"+mahd+"'", cnn);
            cmd.CommandType = CommandType.Text;
            cnn.Open();
            cmd.ExecuteNonQuery();
            cnn.Close();
        }
        
    }
}
